﻿using HRMSystem.Models;
using HRMSystem.Wpf.WpfWindow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HRMSystem.Wpf.CompanyPages
{
    /// <summary>
    /// Interaction logic for viewPage.xaml
    /// </summary>
    public partial class EditPage : Page
    {
        private static EditPage instance;
        List<CompanyDetails> list, hoList;
        CompanyDetails editDetails;
        

        public EditPage()
        {
            InitializeComponent();
            csd.DisplayDateEnd = DateTime.Today;

           // list = DBServices.CompanyService.findCompanyDetails(Session.Manager.ID);
            list = DBServices.CompanyService.findCompanyDetails(1);
            hoList = DBServices.CompanyService.findHeadOffice(list.ElementAt(0).CID);

            name.Text = list.ElementAt(0).NAME;
            regNo.Text = list.ElementAt(0).REG_NO;
            vision.Text = list.ElementAt(0).VISION;
            mission.Text = list.ElementAt(0).MISSION;
            csd.Text = list.ElementAt(0).START_DATE.Value.ToShortDateString();
            hoa.Text = hoList.ElementAt(0).HOADDRESS;
            yba.Text = list.ElementAt(0).YOURADDRESS;
            department.Text = list.ElementAt(0).DEPARTMENTNAME;
        }

        public static EditPage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new EditPage();
                }

                return instance;
            }
        }

     
        private void Cancel_Button_Click_1(object sender, RoutedEventArgs e)
        {
            CompanyPage.Instance.ContentFrame.Content = ViewPage.Instance;
        }

        private void Save_Button_Click_1(object sender, RoutedEventArgs e)
        {
            editDetails = new CompanyDetails();

            if (hoList.ElementAt(0).HOADDRESS != hoa.Text)
            {
                DBServices.CompanyService.UpdateHOAddress(list.ElementAt(0).CID, hoa.Text);

            }

            editDetails.CID = list.ElementAt(0).CID;
            editDetails.NAME = name.Text;
            editDetails.REG_NO = regNo.Text;
            editDetails.VISION = vision.Text;
            editDetails.MISSION = mission.Text;
            editDetails.START_DATE = Convert.ToDateTime(csd.Text);
            editDetails.YOURADDRESS =   yba.Text;
            editDetails.DEPARTMENTID = list.ElementAt(0).DEPARTMENTID;
            editDetails.DEPARTMENTNAME = department.Text;

           // DBServices.CompanyService.updateCompanyDetails(editDetails, Session.Manager.ID);
            DBServices.CompanyService.updateCompanyDetails(editDetails, 1);

            ViewPage.NullInstance();
            CompanyPage.Instance.ContentFrame.Content = ViewPage.Instance;
            EditPage.NullInstance();

        }

        public static void NullInstance()
        {
            instance = new EditPage();
        }

    }
}
